package com.wq.payunion.oauth2.useroauth2.controller;

import com.wq.payunion.oauth2.useroauth2.constant.AuthorizationConfig;
import com.wq.payunion.oauth2.useroauth2.exceptions.OAuthCheckException;
import com.wq.payunion.oauth2.useroauth2.model.AbstractOAuthBean;
import com.wq.payunion.oauth2.useroauth2.model.OAuthInfoParam;
import com.wq.payunion.oauth2.useroauth2.model.OAuthResult;
import com.wq.payunion.oauth2.useroauth2.model.common.CommonResult;
import com.wq.payunion.oauth2.useroauth2.oauth2.OAuthTemplate;
import com.wq.payunion.oauth2.useroauth2.oauth2.ThirdSystemOAuth;
import com.wq.payunion.oauth2.useroauth2.utils.ApplicationContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wangqiang20995
 * @Date:2018 年 9 月 8 日 0008
 * @Description:
 * @Resource:
 */
@Controller
@RequestMapping("unionAuth")
public class UnionAuthController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AuthorizationConfig authorizationConfig;

    @Autowired
    private OAuthTemplate oAuthTemplate;



    @ResponseBody
    @RequestMapping("/version")
    public Map<String,Object> version(String param){
        Map<String,Object> map = new HashMap<>();
        map.put("flag","ok");
        map.put("info","query version successfully");
        map.put("version","1.0.0");
        logger.info("return data --> {}",map);
        return map;
    }

    @ResponseBody
    @RequestMapping("/authorizationCode")
    public CommonResult authorizationCode(String authorizeType,String extractParams){
        CommonResult commonResult = new CommonResult();
        logger.info("调用oauth鉴权系统[{}]",authorizeType);
        String service = authorizationConfig.getServiceMap(authorizeType);
        ThirdSystemOAuth baiduAuthorize = ApplicationContextUtils.getBean(service,ThirdSystemOAuth.class);
        if(baiduAuthorize == null){
            throw new OAuthCheckException("9999","尚未找到对应系统的OAuth2.0服务");
        }
        this.oAuthTemplate.setThirdSystemOAuth(baiduAuthorize);
        OAuthResult authResult = this.oAuthTemplate.executeAuthorize(this.authorizationConfig.getServiceMap(authorizeType),extractParams);
        commonResult.setServiceResult("data",authResult);
        return commonResult;
    }


    @ResponseBody
    @RequestMapping("/accessTokenByCode")
    public CommonResult accessTokenByCode(String requestOs,String uuid,String params){
        logger.info("调用Oauth2授权系统[{}]进行授权",requestOs);
        String service = authorizationConfig.getServiceMap(requestOs);
        ThirdSystemOAuth thirdSystemOAuth = ApplicationContextUtils.getBean(service,ThirdSystemOAuth.class);
        if(thirdSystemOAuth == null){
            throw new OAuthCheckException("9999","尚未找到对应系统的OAuth2.0服务");
        }

        if(StringUtils.isEmpty(params)){
            throw new OAuthCheckException("9999","params参数应该为json字符串，而且必须包含key:code和redirect_url");
        }
        this.oAuthTemplate.setThirdSystemOAuth(thirdSystemOAuth);
        AbstractOAuthBean abstractOAuthBean = new AbstractOAuthBean();
        abstractOAuthBean.setUuid(uuid);
        abstractOAuthBean.setOauthName(requestOs);
        OAuthResult oAuthResult = this.oAuthTemplate.executeAuthorize(abstractOAuthBean,params);

        CommonResult commonResult = new CommonResult();
        commonResult.setServiceResult("data",oAuthResult);
        return commonResult;
    }

    @ResponseBody
    @RequestMapping("/loggedUserInfo")
    public CommonResult loggedUserInfo(String accessToken,String requestOs){
        logger.info("调用OAuth2.0授权系统，根据AccessToken获取授权基本信息");
        String service = authorizationConfig.getServiceMap(requestOs);
        ThirdSystemOAuth thirdSystemOAuth = ApplicationContextUtils.getBean(service,ThirdSystemOAuth.class);
        if(thirdSystemOAuth == null){
            throw new OAuthCheckException("9999","尚未找到对应系统的OAuth2.0服务");
        }

        this.oAuthTemplate.setThirdSystemOAuth(thirdSystemOAuth);

        AbstractOAuthBean oAuthBean = new OAuthInfoParam();
        oAuthBean.setOauthName(requestOs);
        Map<String,String> param = new HashMap<>();
        param.put("access_token",accessToken);
        ((OAuthInfoParam) oAuthBean).setExtractParams(param);
        OAuthResult oAuthResult = this.oAuthTemplate.executeAuthorize(oAuthBean,"");

        CommonResult commonResult = new CommonResult();
        commonResult.setServiceResult("data",oAuthResult);

        return commonResult;
    }
}
